package com.polaris.live.ui.main.live

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.polaris.live.R
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.databinding.FragmentLiveFollowMainBinding
import com.polaris.live.utils.interfaces.OnAutoRefreshListener

/**
 * LiveFollowMainFragment
 *
 * @author Created by 天晴 on 2024/12/23/005 9:39
 */
class LiveFollowMainFragment : BaseVbFragment<BaseViewModel, FragmentLiveFollowMainBinding>(),
    OnAutoRefreshListener {

    private val mFragments = mutableListOf<Fragment>()

    private val mLiveFollowListFragment by lazy {
        LiveFollowListFragment()
    }
    private val mFriendRoomFragment by lazy {
        LiveFollowRoomFragment()
    }

    override fun initView(savedInstanceState: Bundle?) {
        initListener()
        mFragments.add(mLiveFollowListFragment)
        mFragments.add(mFriendRoomFragment)
        mBinding.viewpager2.adapter = ViewPagerAdapter(mFragments, this)
        changePage(0)
    }

    private fun initListener() {
        mBinding.tvFollow.setOnSingleClickListener {
            changePage(0)
            mBinding.viewpager2.currentItem = 0
            hiddenChanged(0)
        }
        mBinding.tvRecent.setOnSingleClickListener {
            mBinding.viewpager2.currentItem = 1
            changePage(1)
            hiddenChanged(1)
        }
        mBinding.viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                changePage(position)
                hiddenChanged(position)
            }
        })
    }

    private fun changePage(position: Int) {
        when (position) {
            0 -> {
                mBinding.tvFollow.isSelected = true
                mBinding.tvRecent.isSelected = false
                mBinding.tvFollow.setBackgroundResource(R.drawable.friend_ship_tab_select_back)
                mBinding.tvRecent.setBackgroundResource(R.drawable.friend_ship_tab_not_select_back)
            }

            1 -> {
                mBinding.tvFollow.isSelected = false
                mBinding.tvRecent.isSelected = true
                mBinding.tvRecent.setBackgroundResource(R.drawable.friend_ship_tab_select_back)
                mBinding.tvFollow.setBackgroundResource(R.drawable.friend_ship_tab_not_select_back)
            }
        }
    }

    private fun hiddenChanged(position: Int) {
        mFragments.forEachIndexed { index, fragment ->
            val show = index != position
            fragment.onHiddenChanged(show)
        }
    }

    override fun onAutoRefresh() {
        if (!isCreated() || !isLoadData()) {
            return
        }
        val position = mBinding.viewpager2.currentItem
        if (position == 0) {
            mLiveFollowListFragment.onAutoRefresh()
        } else if (position == 1) {
            mFriendRoomFragment.onAutoRefresh()
        }
    }
}